const api = require('./api');
function ajax(url, data, fn){
  wx.request({
    url: api[url],
    data: data,
    header: {
      'content-type': 'application/x-www-form-urlencoded', // 默认值
      'token': wx.getStorageSync('userData').token || ''
    },
    success: function (res) {
      fn && fn(res);
    },
    fail: function (res) {
      fn && fn(res);
    },
    complete: function (res) {
    },
  })
}

function __args() {
	var setting = {};
  setting.url = api[arguments[0]];
	if (arguments.length === 1 && typeof arguments[0] !== 'string') {
		setting = arguments[0];
	} else {
		if (typeof arguments[1] === 'object') {
			setting.data = arguments[1];
			setting.success = arguments[2];
		} else {
			setting.success = arguments[1];
		}
	}
	return setting;
}
function __json(method, setting) {
	setting.method = method;
	setting.header = {
    'content-type': 'application/json',
		'token': wx.getStorageSync('token'),
	};
	wx.request(setting);
}

module.exports = {
	getJSON: function () {
		__json('GET', __args.apply(this, arguments));
	},
	postJSON: function () {
		__json('POST', __args.apply(this, arguments));
	}
}
